package com.usian.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.UserRelationDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.mapper.ApUserFanMapper;
import com.usian.user.mapper.ApUserFollowMapper;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.service.ApUserFollowService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserFollowServiceImpl implements ApUserFollowService {

    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;
    @Autowired
    private ApUserFanMapper apUserFanMapper;
    @Autowired
    private ApAuthorFeign apAuthorFeign;
    @Autowired
    private KafkaTemplate kafkaTemplate;


    @Override
    public ResponseResult userFollow(UserRelationDto dto) {

        if(dto==null || dto.getOperation()==null ||dto.getArticleId()==null ||dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取当前登录用户信息
        Integer apUserId =null;
        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser!=null){
            apUserId = apUser.getId();
        }


        //状态判断 0关注  1取消关注
        if(dto.getOperation()==0){
            //关注
            this.guanzhu(apUserId,dto);
            //关注成功后发送消息去记录用户关注行为
            FollowBehaviorDto dto1 =new FollowBehaviorDto();
            dto1.setUserId(apUserId);
            dto1.setFollowId(dto.getAuthorId());
            dto1.setArticleId(dto.getArticleId());

            kafkaTemplate.send("followBehavior", JSON.toJSONString(dto1));

        }else if(dto.getOperation()==1){
            //取关
            this.quguan(apUserId,dto);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private ResponseResult guanzhu(Integer apuserId,UserRelationDto dto){

        //1：要关注的人是否存在
        //ApUser apUser = apUserMapper.selectById(dto.getAuthorId());
        //if(apUser ==null){
        //    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        //}

        //查询当前登录用户详情
        ApUser apUser1 = apUserMapper.selectById(apuserId);
        if(apUser1 ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"当前登录用户失效");
        }

        //远程调用获取作者详情
        ApAuthor apAuthor = apAuthorFeign.findById(dto.getAuthorId());
        if(apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"当前关注作者不存在");
        }


        //查看关注人是否存在
        ApUser apUser = apUserMapper.selectById(apAuthor.getUserId());
        if(apUser ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"当前关注作者不存在");
        }

        //2: 是够已关注
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getUserId, apuserId).eq(ApUserFollow::getFollowId, dto.getAuthorId()));
        if(apUserFollow ==null){
            //3: 关注用户  insert
            apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(apuserId);
            apUserFollow.setFollowId(dto.getAuthorId());
            apUserFollow.setFollowName(apAuthor.getName());
            apUserFollow.setCreatedTime(new Date());
            apUserFollow.setIsNotice(true);
            apUserFollow.setLevel((short)1);
            apUserFollowMapper.insert(apUserFollow);

            //维护粉丝表
            ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId, apuserId).eq(ApUserFan::getUserId, dto.getAuthorId()));
            if(apUserFan ==null){
                apUserFan = new ApUserFan();
                apUserFan.setUserId(dto.getAuthorId());
                apUserFan.setFansId(Long.valueOf(apuserId.toString()));
                apUserFan.setFansName(apUser1.getName());
                apUserFan.setLevel((short)0);
                apUserFan.setIsDisplay(true);
                apUserFan.setIsShieldLetter(false);
                apUserFan.setIsShieldComment(false);
                apUserFan.setCreatedTime(new Date());
                apUserFanMapper.insert(apUserFan);
            }

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private ResponseResult quguan(Integer apuserId,UserRelationDto dto){

        //删 follow
        apUserFollowMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getUserId, apuserId).eq(ApUserFollow::getFollowId, dto.getAuthorId()));
        //删 fans
        apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId, apuserId).eq(ApUserFan::getUserId, dto.getAuthorId()));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }


}
